﻿using MindASPCoreWebAPI.BaseLibrary.Tools.TypeTools;
using MindASPCoreWebAPI.DtoModel.Common;
using MindASPCoreWebAPI.DtoModel.SystemManager;
using MindASPCoreWebAPI.IRepository.SystemManager;
using MindASPCoreWebAPI.PoModel.SystemManager;
using MindASPCoreWebAPI.Repository.CommonLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MindASPCoreWebAPI.Repository.SystemManager
{
    public class ClientAdminRepository:BaseRepository, IClientAdminRepository
    {
        public SystemCenterMindContext _systemCenterMindContext;
        public ClientAdminRepository(SystemCenterMindContext systemCenterMindContext) : base(systemCenterMindContext)
        {
            _systemCenterMindContext = systemCenterMindContext;
        }

        /// <summary>
        /// 活动管理表显示
        /// </summary>
        /// <returns></returns>
        public PagerResult<ClientAdminDto> ClientAdminShow(int hang, int ka, int qu, int fen, int shi, int ad, string gong, string xname, int pageIndex, int pageSize)
        {
            var client = _systemCenterMindContext.CustAdminModel;
            var posi = _systemCenterMindContext.PosiModel;
            var level = _systemCenterMindContext.LevelModel;
            var area = _systemCenterMindContext.AreaModel;
            var acth = _systemCenterMindContext.ActhModel;
            var lang = _systemCenterMindContext.LangModel;
            var com = _systemCenterMindContext.CompanyAdminModel;
            var ind = _systemCenterMindContext.IndustryAdminModel;
            var kad = _systemCenterMindContext.KaModel;
            var list = from a in client
                       join b in posi on a.posi_id equals b.posi_id
                       join c in level on a.leve_id equals c.leve_id
                       join d in acth on a.auth_id equals d.auth_id
                       join e in lang on a.lang_id equals e.lang_id
                       join f in com on a.com_id equals f.com_id
                       join g in area on a.area_id equals g.area_id
                       join h in ind on a.com_ind_id equals h.com_ind_id
                       join i in kad on f.com_ka_id equals i.com_ka_id
                       select new ClientAdminDto
                       {
                           cust_id=a.cust_id,
                           cust_name=a.cust_name,
                           cust_englishname=a.cust_englishname,
                           cust_sex=a.cust_sex,
                           com_id=f.com_id,
                           com_name = f.com_name,
                           //com_ind_id = f.com_ind_id,
                           com_state = f.com_state,
                           nat_id = f.nat_id,
                           com_ka_id = i.com_ka_id,
                           com_ka_name=i.com_ka_name,
                           com_mp4 = f.com_mp4,
                           com_ad = f.com_ad,
                           com_site = f.com_site,
                           com_order = f.com_order,
                           area_id=g.area_id,
                           area_name=g.area_name,
                           cust_depa=a.cust_depa,
                           posi_id=b.posi_id,
                           posi_name=b.posi_name,
                           cust_deta=a.cust_deta,
                           leve_id=c.leve_id,
                           leve_name=c.leve_name,
                           auth_id=d.auth_id,
                           auth_name=d.auth_name,
                           lang_id=e.lang_id,
                           lang_name=e.lang_name,
                           lang_workphone = a.lang_workphone,
                           cust_email = a.cust_email,
                           cust_phone = a.cust_phone,
                           cust_birt = a.cust_birt,
                           cust_postal = a.cust_postal,
                           cust_market = a.cust_market,
                           cust_hobby = a.cust_hobby,
                           cust_desc = a.cust_desc,
                           cust_wecha = a.cust_wecha,
                           cust_create = a.cust_create,
                           cust_modi = a.cust_modi,
                           cust_remark = a.cust_remark,
                           com_ind_id=h.com_ind_id,
                           com_ind_name=h.com_ind_name,
                           com_ind_order=h.com_ind_order,
                           com_ind_status=h.com_ind_status,
                           cust_update_name=a.cust_update_name,
                           cust_update_date=a.cust_update_date

                       };
            if(hang>0)
            {
                list = list.Where(c => c.com_ind_id == hang);
            }
            if (ka > 0)
            {
                list = list.Where(c => c.com_ka_id == ka);
            }
            if (qu > 0)
            {
                list = list.Where(c => c.area_id == qu);
            }
            if (fen > 0)
            {
                list = list.Where(c => c.leve_id == fen);
            }
            if (shi > 0)
            {
                list = list.Where(c => c.com_mp4 == shi);
            }
            if (ad > 0)
            {
                list = list.Where(c => c.com_ad == ad);
            }
            if(gong!=null)
            {
                list = list.Where(c => c.com_name == gong);
            }
            if (xname != null)
            {
                list = list.Where(c => c.cust_name == xname);
            }
            var total = list.Count();
            // list = list.OrderBy(a => a.cust_id).Skip((pageIdnex - 1) * pageSize).Take(pageSize);
            //return (List<ClientAdminDto>)list;
            // return list.ToList();
            PagerResult<ClientAdminDto> result = new PagerResult<ClientAdminDto>()
            {
                PageCount = (int)Math.Ceiling((decimal)total / pageSize),
                Total = total,
                Data = list.OrderBy(c => c.cust_id).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(),
                PageIndex = pageIndex,
            };
            
            return result;

            
        }

        /// <summary>
        /// 行业下拉
        /// </summary>
        /// <returns></returns>
        public List<IndustryAdminModel> Inddown()
        {
            var list = _systemCenterMindContext.IndustryAdminModel.ToList();
            return list;
        }
        /// <summary>
        /// Ka下拉
        /// </summary>
        /// <returns></returns>
        public List<KaModel> Kadown()
        {
            var list = _systemCenterMindContext.KaModel.ToList();
            return list;
        }
        /// <summary>
        /// 区域下拉
        /// </summary>
        /// <returns></returns>
        public List<AreaModel> Areadown()
        {
            var list = _systemCenterMindContext.AreaModel.ToList();
            return list;
        }

        /// <summary>
        /// 分级下拉
        /// </summary>
        /// <returns></returns>
        public List<LevelModel> Leveldown()
        {
            var list = _systemCenterMindContext.LevelModel.ToList();
            return list;
        }

        /// <summary>
        /// 客户管理删除
        /// </summary>
        /// <param name="cust_id"></param>
        /// <returns></returns>
        public int Cust_Delete(int cust_id)
        {
            var list = _systemCenterMindContext.CustAdminModel.FirstOrDefault(c => c.cust_id == cust_id);
            if(list==null)
            {
                return 3;
            }
            _systemCenterMindContext.Remove(list);
            return _systemCenterMindContext.SaveChanges();
        }
        /// <summary>
        /// 添加反填
        /// </summary>
        /// <param name="hang"></param>
        /// <param name="ka"></param>
        /// <param name="qu"></param>
        /// <param name="fen"></param>
        /// <param name="shi"></param>
        /// <param name="ad"></param>
        /// <param name="gong"></param>
        /// <param name="xname"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public List<ClientAdminDto> Show(int cust_id)
        {
            var client = _systemCenterMindContext.CustAdminModel;
            var posi = _systemCenterMindContext.PosiModel;
            var level = _systemCenterMindContext.LevelModel;
            var area = _systemCenterMindContext.AreaModel;
            var acth = _systemCenterMindContext.ActhModel;
            var lang = _systemCenterMindContext.LangModel;
            var com = _systemCenterMindContext.CompanyAdminModel;
            var ind = _systemCenterMindContext.IndustryAdminModel;
            var kad = _systemCenterMindContext.KaModel;
            var list = from a in client
                       join b in posi on a.posi_id equals b.posi_id
                       join c in level on a.leve_id equals c.leve_id
                       join d in acth on a.auth_id equals d.auth_id
                       join e in lang on a.lang_id equals e.lang_id
                       join f in com on a.com_id equals f.com_id
                       join g in area on a.area_id equals g.area_id
                       join h in ind on a.com_ind_id equals h.com_ind_id
                       //join i in kad on a.com_ka_id equals i.com_ka_id
                       select new ClientAdminDto
                       {
                           cust_id = a.cust_id,
                           cust_name = a.cust_name,
                           cust_englishname = a.cust_englishname,
                           cust_sex = a.cust_sex,
                           com_id = f.com_id,
                           com_name = f.com_name,
                           //com_ind_id = f.com_ind_id,
                           com_state = f.com_state,
                           nat_id = f.nat_id,
                          // com_ka_id = i.com_ka_id,
                           //com_ka_name = i.com_ka_name,
                           com_mp4 = f.com_mp4,
                           com_ad = f.com_ad,
                           com_site = f.com_site,
                           com_order = f.com_order,
                           area_id = g.area_id,
                           area_name = g.area_name,
                           cust_depa = a.cust_depa,
                           posi_id = b.posi_id,
                           posi_name = b.posi_name,
                           cust_deta = a.cust_deta,
                           leve_id = c.leve_id,
                           leve_name = c.leve_name,
                           auth_id = d.auth_id,
                           auth_name = d.auth_name,
                           lang_id = e.lang_id,
                           lang_name = e.lang_name,
                           lang_workphone = a.lang_workphone,
                           cust_email = a.cust_email,
                           cust_phone = a.cust_phone,
                           cust_birt = a.cust_birt,
                           cust_postal = a.cust_postal,
                           cust_market = a.cust_market,
                           cust_hobby = a.cust_hobby,
                           cust_desc = a.cust_desc,
                           cust_wecha = a.cust_wecha,
                           cust_create = a.cust_create,
                           cust_modi = a.cust_modi,
                           cust_remark = a.cust_remark,
                           com_ind_id = h.com_ind_id,
                           com_ind_name = h.com_ind_name,
                           com_ind_order = h.com_ind_order,
                           com_ind_status = h.com_ind_status,
                           cust_update_name = a.cust_update_name,
                           cust_update_date = a.cust_update_date

                       };

            List<ClientAdminDto> aa = list.Where(c => c.cust_id == cust_id).ToList();

            return aa;


        }
        /// <summary>
        /// 公司性质下拉
        /// </summary>
        /// <returns></returns>
        public List<NatModel> Natdown()
        {
            var list = _systemCenterMindContext.NatModel.ToList();
            return list;
        }

        /// <summary>
        /// 客户职位下拉
        /// </summary>
        /// <returns></returns>
        public List<PosiModel> Posidown()
        {
            var list = _systemCenterMindContext.PosiModel.ToList();
            return list;
        }
        /// <summary>
        /// 权力决策下拉
        /// </summary>
        /// <returns></returns>
        public List<ActhModel> Acthdown()
        {
            var list = _systemCenterMindContext.ActhModel.ToList();
            return list;
        }

        /// <summary>
        /// 语言下拉
        /// </summary>
        /// <returns></returns>
        public List<LangModel> Langdown()
        {
            var list = _systemCenterMindContext.LangModel.ToList();
            return list;
        }
        /// <summary>
        /// 公司显示
        /// </summary>
        /// <returns></returns>
        public List<CompanyAdminModel> CompList(string comname)
        {
            var list = _systemCenterMindContext.CompanyAdminModel.Where(c => c.com_name == comname).ToList();
            return list;
            
        }
        /// <summary>
        /// 客户添加
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public int ClientAdd(CustAdminModel list)
        {

            CustAdminModel data = new CustAdminModel()
            {
                cust_name = list.cust_name,
                cust_englishname = list.cust_englishname,
                cust_sex = list.cust_sex,
                com_id = list.com_id,
                com_ind_id = list.com_ind_id,
                area_id = list.area_id,
                cust_depa = list.cust_depa,
                posi_id = list.posi_id,
                cust_deta = list.cust_deta,
                leve_id = list.leve_id,
                auth_id = list.auth_id,
                lang_id = list.lang_id,
                lang_workphone = list.lang_workphone,
                //com_ka_id = list.com_ka_id,
                cust_phone = list.cust_phone,
                cust_email = list.cust_email,
                cust_birt = list.cust_birt,
                cust_postal = list.cust_postal,
                cust_market = list.cust_market,
                cust_hobby = list.cust_hobby,
                cust_desc = list.cust_desc,
                cust_wecha = list.cust_wecha,
                cust_create = list.cust_create,
                cust_modi = list.cust_modi,
                cust_remark = list.cust_remark,
                cust_update_name = "康旭",
                cust_update_date = DateTime.Now

            };
            _systemCenterMindContext.Add(data);
            _systemCenterMindContext.SaveChanges();
            return 1;
        }
        /// <summary>
        /// 客户管理编辑反填
        /// </summary>
        /// <param name="cust_id"></param>
        /// <returns></returns>
        //public CustAdminModel EditShow(int cust_id)
        //{
        //    var list = _systemCenterMindContext.CustAdminModel.FirstOrDefault(c => c.cust_id == cust_id);
        //    return list;
        //}
        /// <summary>
        /// 编辑反填公司字段
        /// </summary>
        /// <param name="com_id"></param>
        /// <returns></returns>
        //public CompanyAdminModel ComList(int com_id)
        //{
        //    var list = _systemCenterMindContext.CompanyAdminModel.FirstOrDefault(c => c.com_id == com_id);
        //    return list;
        //}
        /// <summary>
        /// 客户管理反填
        /// </summary>
        /// <param name="cust_id"></param>
        /// <returns></returns>
        public EditShow EditeList(int cust_id)
        {
            var cam = _systemCenterMindContext.CustAdminModel.FirstOrDefault(c => c.cust_id == cust_id);
            var companyAdminModel = _systemCenterMindContext.CompanyAdminModel.FirstOrDefault(c => c.com_id == cam.com_id);
            var es = new EditShow()
            {
                cust_name = cam.cust_name,
                cust_englishname = cam.cust_englishname,
                cust_sex = cam.cust_sex,
                com_id = companyAdminModel.com_id,
                com_name = companyAdminModel.com_name,
                com_ind_id = cam.com_ind_id,
                area_id = cam.area_id,
                cust_depa = cam.cust_depa,
                posi_id = cam.posi_id,
                cust_deta = cam.cust_deta,
                leve_id = cam.leve_id,
                auth_id = cam.auth_id,
                lang_id = cam.lang_id,
                lang_workphone = cam.lang_workphone,
                cust_phone = cam.cust_phone,
                cust_email = cam.cust_email,
                cust_birt = cam.cust_birt,
                cust_postal = cam.cust_postal,
                cust_market = cam.cust_market,
                cust_hobby = cam.cust_hobby,
                cust_desc = cam.cust_desc,
                cust_wecha = cam.cust_wecha,
                cust_create = cam.cust_create,
                cust_modi = cam.cust_modi,
                cust_remark = cam.cust_remark,
            };
            return es;
           
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="cust"></param>
        /// <returns></returns>
        public int EditXiu(CustAdminModel cust)
        {
            var list = _systemCenterMindContext.CustAdminModel.FirstOrDefault(c => c.cust_id == cust.cust_id);
            ModelCopier.CopyModel(cust, list);
            return _systemCenterMindContext.SaveChanges();
        }
        /// <summary>
        /// 字典表
        /// </summary>
        /// <returns></returns>
        public List<DictiDto> DictiShow(string dt_type_key)
        {
            var list = from a in _systemCenterMindContext.Dictionary_Model
                       join b in _systemCenterMindContext.DictionaryTypeTablePo
                       on a.dt_type_key equals b.dt_type_key
                       select new DictiDto
                       {
                           dt_key=a.dt_key,
                           dt_type_key=b.dt_type_key,
                           dt_name=a.dt_name,
                           dt_status=a.dt_status
                       };
            if(dt_type_key!=null)
            {
                list = list.Where(c => c.dt_type_key == dt_type_key);
            }
            return list.ToList();
        }

    }
}
